package com.infinite.itzone.util;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.Toast;

/**
 * 在线客服
 * Created by Administrator on 2018/9/7.
 */

public class GMOnlineCustomerUtil {

    public static void openQqCustomerService(Context context, String url){
        try {
            String qqNumber = url.substring(url.indexOf("&uin=")+5, url.indexOf("&website"));
            openQqCustomerServiceByQQNumber(context, qqNumber);
        } catch (Exception e) {
            showTips(context,"获取客服信息失败,请稍候再试");
        }
    }
    public static void openQqCustomerServiceByQQNumber(Context context, String qqNumber){
        try {
            if (TextUtils.isEmpty(qqNumber)){
                showTips(context, "获取客服信息失败,请稍候再试");
                return;
            }
            if(UtilApp.isInstallApp(context, "com.tencent.mobileqq")) {
                String url2 = "mqqwpa://im/chat?chat_type=wpa&uin=" + qqNumber;
                context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url2)));
            } else {
                showTips(context,"您还没有安装QQ,请安装最新版QQ");
            }
        } catch (ActivityNotFoundException e) {
            showTips(context, "请安装最新版QQ！");
        }
    }

    public static void openQqRecently(Context context){
        try {
            if(UtilApp.isInstallApp(context, "com.tencent.mobileqq")) {
                Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.tencent.mobileqq");
                context.startActivity(intent);
            } else {
                showTips(context,"您还没有安装QQ,请安装最新版QQ");
            }
        } catch (ActivityNotFoundException e) {
            showTips(context, "请安装最新版QQ！");
        }
    }

    private static void showTips(Context context, String msg){
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }
}
